#ifndef __CAN_H_
#define __CAN_H_

/**
  ******************************************************************************
  * @file    can.h
  * @author  Dajin Li
  * @version V0.0.1
  * @date    08-08-2015 
  * @brief   This file contains CANopen specific functions.
  *          
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; KION(SZ) R&D Center
  *
  * Licensed under KION, (the "License");
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "stm32f10x_can.h"
#include "global.h"
#include "CAN_canopen.h"

/*****************************************************************************************
Macros
******************************************************************************************/
#define FILTER_NUMBER_DEF                      14

#define GPIO_CANOPEN                           GPIOA  
#define GPIO_Pin_CANOPEN_RX                    GPIO_Pin_11
#define GPIO_Pin_CANOPEN_TX                    GPIO_Pin_12

#define CANOPEN_CAN                            CAN1

#define CANOPEN_CAN_FIFO                       CAN_FIFO0

#define CAN_MASTER_ID_DEF                      0  //default master id

#define CAN_SLAVE_ID_DEF                       4 //default slave id 

#define CANOPEN_RX_MSG_BUF          128
#define CANOPEN_CAN                 CAN1
#define CANOPEN_CAN_FIFO            CAN_FIFO0
#define CANOPEN_CAN_RX_IRQ          USB_LP_CAN1_RX0_IRQn

#define CANOPEN_FAULT_DELAY         100

#define CAN_RX_MSG_BUF                      128


#define NMT                         0x0     // Network Management
#define SYNC_ERR                    0x1     // Both Sync (COB-ID=0) & Error (COB-ID=Error)
#define RESV_1                      0x2
#define PDO_MISO                    0x3     // Process Data Object (Master In  Slave Out)
#define PDO_MOSI                    0x4     // Process Data Object (Master Out Slave In)
#define PDO_SYS1                    0x5     // Process Data Object (System Broadcast 1)
#define PDO_SYS2                    0x6     // Process Data Object (System Broadcast 2)
#define PDO_SYS3                    0x7     // Process Data Object (System Broadcast 3)
#define PDO_SYS4                    0x8     // Process Data Object (System Broadcast 4)
#define IDO_MISO                    0x9     // Index Data Object (Master In  Slave Out)
#define IDO_MOSI                    0xA     // Index Data Object (Master Out Slave In)
#define SDO_MISO                    0xB     // Service Data Object (Master In  Slave Out)
#define SDO_MOSI                    0xC     // Service Data Object (Master Out Slave In)
#define RESV_2                      0xD
#define NODE                        0xE     // Node Guard Message
#define RESV_3                      0xF
#define CAN_DATA_LEN_DEF            8

#define HEARTBEAT		            0xE     // Heartbeat


#define CANOPEN_FILTER_NUMBER_0                0
#define CANOPEN_FILTER_NUMBER_1                1
#define CANOPEN_FILTER_NUMBER_2                2
#define CANOPEN_FILTER_NUMBER_3                3
#define CANOPEN_FILTER_NUMBER_4                4
#define CANOPEN_FILTER_NUMBER_5                5
#define CANOPEN_FILTER_NUMBER_6                6
#define CANOPEN_FILTER_NUMBER_7                7
#define CANOPEN_FILTER_NUMBER_8                8
#define CANOPEN_FILTER_NUMBER_9                9
#define CANOPEN_FILTER_NUMBER_10               10
#define CANOPEN_FILTER_NUMBER_11               11
#define CANOPEN_FILTER_NUMBER_12               12
#define CANOPEN_FILTER_NUMBER_13               13

#define CAN_BOOTUP                             0
#define CAN_STOPED                             4
#define CAN_OPERATIONAL                        5
#define CAN_PRE_OPERATIONAL                    127

extern CanRxMsg CANRxMsgBuf[CAN_RX_MSG_BUF];
extern u8 CAN_rx_buf_head;
extern u8 CAN_rx_buf_tail;

extern u16 can_node_id;
extern s16 can_baud_rate;
extern s16 can_bus_on;                 // CAN Bus Status (0=off, non-0=on)
extern s16 can_error_status;           // Error Status: Bit0=EWRN, Bit1=BOFF
extern s16 working_can_slave_id;
extern s16 working_can_master_id;

extern void CANopen_NVIC_Config(void);
extern void CAN_Config(void);
extern u8 CAN_Set_Baudrate(u8 rate);
static void check_busoff(void);
extern u16 make_cob_id(u16 type, u16 master_id, u16 slave_id);

#endif

